import { modal } from 'vue-strap'
import { showMsg, getInvesterDetail, getInvesterList, updateHkGroup } from '../../../../../vuex/action.js'

export default {
  data() {
    return {
      serverRoot: server.root,
      showUpdateInvesterModal: false,
      detail: {},
      investerList: []
    }
  },
  props: ['id'],
  computed: {
    currentInvester() {
      let currentInvester = ''
      this.investerList.forEach((item) => {
        if (!this.detail.investerId) {
          this.detail.investerId = item.id
        }
        if (+item.id === +this.detail.investerId) {
          currentInvester = item
        }
      })
      return currentInvester
    }
  },
  methods: {
    checkNullOrUndefined(val) {
      if (val === null || val === undefined) {
        return ''
      }
      return val
    },
    trim(str) {
      str = this.checkNullOrUndefined(str)
      if (typeof str === 'string') {
        return str.replace(/(^ +)|( +$)/g, '')
      }
      return str
		},
    validCheck() {
      let inputs = ['id', 'name', 'descr', 'betaType', 'proDesc', 'investerId']
      let key
      let detail = this.detail
      for (key of inputs) {
        if (!detail[key] || detail[key].length === 0) {
          return '* 标记的都为必填项'
        }
      }
    },
    updateInvesterDetail() {
      let validMsg = this.validCheck()
      if (validMsg) {
        return this.xShowMsg({
          type: 'danger',
          content: validMsg
        })
      }

      let formData = new FormData()
      let files = this.$els.groupImage.files[0]
      Object.keys(this.detail).forEach((key) => {
        if (key === 'file' && files) {
          formData.append('file', files)
        } else {
          formData.append(key, this.trim(this.detail[key]))
        }
      })
      // 如果未修改组合封面
      if (!files) {
        formData.append('oldUrl', this.detail['imgUrl'])
      }
      formData.append('type', this.payType)

      this.xUpdateHkGroup(formData)
        .then(() => {
          this.showUpdateInvesterModal = false
          this.$dispatch('on_refresh_list')
          this.$els.addForm.reset()
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    showUpdateInvesterDialog() {
      this.getInvesterList() // 请求投顾列表信息
      this.xGetInvesterDetail({
        id: this.id
      }).then((res) => {
        this.showUpdateInvesterModal = true
        this.detail = res.data.detail
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    getInvesterList() {
      this.xGetInvesterList({
        start: 0,
        limit: 99999
      }).then((res) => {
        this.investerList = res.data.list
      }).catch((res) => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    }
  },
  components: {
    modal: modal
  },
  vuex: {
    getters: {
      payType: state => state.payType
    },
    actions: {
      xShowMsg: showMsg,
      xGetInvesterDetail: getInvesterDetail,
      xGetInvesterList: getInvesterList,
      xUpdateHkGroup: updateHkGroup
    }
  }
}
